package com.fyzk.activity.chongzhi;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.fyzk.base.BaseActivity;
import com.fyzk.bean.RechargeBankTransform;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.utils.ClipUtil;
import com.fyzk.utils.SpanUtils;
import com.fyzk.utils.StringUtils;
import com.fyzk.utils.update.BankTip;
import com.fyzk.utils.update.CozyTip;
import com.fyzk.view.CountDownTimeTextView;
import com.fyzk.view.TopTitleView;
import com.tyjc.lottery.R;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import http.DataUtil.text.TextUtil;

@ContentView(R.layout.activity_bank_transform)
public class BankTransformActivity extends BaseActivity {

    @ViewInject(R.id.chz_top_rl)
    TopTitleView chz_top_rl;
    @ViewInject(R.id.tv_important_tips)
    TextView mTvImportantTips;
    @ViewInject(R.id.tv_money)
    TextView mTvMoney;
    @ViewInject(R.id.tv_copy_money)
    TextView mTvCopyMoney;
    @ViewInject(R.id.tv_name_bank)
    TextView mTvNameBank;
    @ViewInject(R.id.tv_copy_bank)
    TextView mTvCopyBank;
    @ViewInject(R.id.tv_name_bank_card_num)
    TextView mTvNameBankCardNum;
    @ViewInject(R.id.tv_copy_bank_card_num)
    TextView mTvCopyBankCardNum;
    @ViewInject(R.id.tv_name_user)
    TextView mTvNameUser;
    @ViewInject(R.id.tv_copy_user_name)
    TextView mTvCopyUserName;
    @ViewInject(R.id.tv_name_address)
    TextView mTvNameAddress;
    @ViewInject(R.id.tv_copy_address)
    TextView mTvCopyAddress;
    @ViewInject(R.id.tv_count_time)
    CountDownTimeTextView mTvCountTime;

    private RechargeBankTransform bankTransform;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initData();
        initListener();
    }

    private void initData() {
        List<CozyTip> cozyTips = MySharedPreferences.getInstance().getCustomerCalls().getCozyTips();
        List<BankTip> bankTips = MySharedPreferences.getInstance().getCustomerCalls().getBankTips();
        if (bankTips != null) {
            SpanUtils spanUtils = SpanUtils.with(mTvImportantTips);
            for (BankTip bankTip : bankTips) {
                try {
                    spanUtils.appendLine(bankTip.getContent())
                            .setForegroundColor(Color.parseColor(bankTip.getColor().startsWith("#") ?
                                    bankTip.getColor() :
                                    "#" + bankTip.getColor()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            spanUtils.create();
        }

        mTvCountTime.setTimeFormat(CountDownTimeTextView.TIME_SHOW_M_S_CN);
        mTvCountTime.setAutoDisplayText(true);
        CozyTip cozyTip = null;
        if (cozyTips != null && !cozyTips.isEmpty()) {
            cozyTip = cozyTips.get(0);
            String[] seconds = cozyTip.getContent().split("second");
            if (seconds.length > 0) {
                mTvCountTime.setPreText(seconds[0]);
                if (seconds.length > 1)
                    mTvCountTime.setEndText(seconds[1]);
            }
        }
        if (cozyTip != null && !TextUtil.isEmpty(cozyTip.getSecond())) {
            try {
                mTvCountTime.setTimeInFuture(Integer.parseInt(cozyTip.getSecond()) * 1000);
                mTvCountTime.start();
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }

        if (getIntent().hasExtra("bank")) {
            bankTransform = getIntent().getParcelableExtra("bank");
        }

        showData();
    }

    private void initListener() {
        chz_top_rl.setOnTitleClickListener(this);
        mTvCopyMoney.setOnClickListener(this);
        mTvCopyBank.setOnClickListener(this);
        mTvCopyBankCardNum.setOnClickListener(this);
        mTvCopyUserName.setOnClickListener(this);
        mTvCopyAddress.setOnClickListener(this);
    }

    private void showData() {
        if (bankTransform == null)
            return;

        SpanUtils.with(mTvMoney)
                .append(String.format("¥%s", bankTransform.getMoney()))
                .setTypeface(Typeface.MONOSPACE)
                .create();

        mTvNameBank.setText(bankTransform.getBankName());
        mTvNameBankCardNum.setText(StringUtils.getSplitGroup(bankTransform.getCardNum(), 4));
        mTvNameUser.setText(bankTransform.getName());
        mTvNameAddress.setText(bankTransform.getBankAddre());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_copy_money://复制金额
                if (bankTransform != null && !TextUtil.isEmpty(bankTransform.getMoney()))
                    ClipUtil.copy2Clip(getContext(), bankTransform.getMoney());
                break;
            case R.id.tv_copy_bank://复制银行
                if (bankTransform != null && !TextUtil.isEmpty(bankTransform.getBankName()))
                    ClipUtil.copy2Clip(getContext(), bankTransform.getBankName());
                break;
            case R.id.tv_copy_bank_card_num://复制银行卡
                if (bankTransform != null && !TextUtil.isEmpty(bankTransform.getCardNum()))
                    ClipUtil.copy2Clip(getContext(), bankTransform.getCardNum());
                break;
            case R.id.tv_copy_user_name://复制开户姓名
                if (bankTransform != null && !TextUtil.isEmpty(bankTransform.getName()))
                    ClipUtil.copy2Clip(getContext(), bankTransform.getName());
                break;
            case R.id.tv_copy_address://复制地址
                if (bankTransform != null && !TextUtil.isEmpty(bankTransform.getBankAddre()))
                    ClipUtil.copy2Clip(getContext(), bankTransform.getBankAddre());
                break;
        }
    }
}
